/**
* @Title: warning_ctrl.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*
* @Project HIMS-FWA
* @date 2014年10月27日 下午4:43:38
* @version V1.0
*/
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('CaseCheckListCtrl', ['$scope','$state','$http','$modal','BaseURL','Modal','CaseCommonService',
	                                     'CaseAuditListService','FwaTabsetService','InsuranceService','AuditDictServices',
	                                function($scope,$state,$http,$modal,baseUrl,Modal,
	                                		CaseCommonService,caseAuditListService,fwaTabsetService,InsuranceService,AuditDictServices){
		
		
		
		$scope.addOnClick = function(){
			fwaTabsetService.selectTab('caseAudit.caseAuditAdd',null,false);
		}
		
		$scope.auditDictServices = AuditDictServices;
		AuditDictServices.initCodeListDatas();
		
		InsuranceService.initMedicalOrgInfoList().success(function(response){
            $scope.medicalOrgInfoList = response.medicalOrgList;
		});
		
        var cellTemplate = '<a ng-show="data.caseStatus ==5" ng-click="operation(data);">审核</a>' +
            '<a ng-show="data.caseStatus == 6" ng-click="operation(data);">撤回</a>';
		$scope.columns = [
		{
			label : '案件号',
			map : 'caseCode',
			width : '10%',
			 cellTemplate:'<a  ng:bind="data.caseCode" '+
                 'ng-click="queryAuditCase(data)"></a>'
		},
		{
			label : '机构名称',
			map : 'orgName',
			width : '15%'
		}, {
			label : '受理时间',
			map : 'auditDate',
			width : '20%'
		}, {
			label : '不合理费用',
			map : 'totalDeduction',
			width : '10%'
		}, {
			label : '告知时间',
			map : 'notifyDate',
			width : '15%'
		}, {
			label : '机构确认时间',
			map : 'affirmDate',
			width : '10%'
		}, {
			label : '案件状态',
			map : 'statusName',
			width : '10%'
		}, {
			label : '操作',
			map : 'operation',
			width : '10%',
			cellTemplate:cellTemplate}];


		$scope.operation = function(data){

            if(data.caseStatus=="5")
            {
                $state.go('caseCheck.caseCheckProcessList',{caseCode:data.caseCode,problemCode:data.relatedCode,urlFlag:''});
            }else if(data.caseStatus=="6")
            {
            	$state.go('caseCheck.caseCheckProcessList',{caseCode:data.caseCode,problemCode:data.relatedCode,urlFlag:''});
//            	$scope.auditCancelProcess(data.caseCode);
            }else{
            	///查看
            	$scope.queryAuditCase(data);
            }

        }

        $scope.queryAuditCase = function(data){
            $state.go("caseDetailView.detail",{caseCode:data.caseCode,problemCode:data.relatedCode});
        }
		
		$scope.onReset = function(){
            $scope.params.caseCode=null;
            $scope.params.orgName=null;
            $scope.params.auditDate={start:null,end:null};
            $scope.params.sourceType=null;
            $scope.params.doctorName=null;
            $scope.params.jobLicense=null;
            $scope.params.sbmitDate={start:null,end:null};
	        };

            $scope.currentPage = 1;
            var start = 0;
            $scope.limit = 20;
            $scope.params = {
                page:0,
                pageSize:20
            };

            //分页监听
            $scope.$on('pageTool.skipPage',function(e,v){
                $scope.currentPage = v;
                start = $scope.limit*(v-1);
                $scope.onSubmit(true);
            });
            
            
        
            /**
             * 获取勾选的总记录数
             */
            $scope.checkRows = [];
            
            function findCheck(d) {
             
            	for(var i=0;i< $scope.checkRows .length;i++) {
                     var checkedData =  $scope.checkRows [i];
                   if(checkedData.caseCode == d.caseCode) {
                         return i;
                     }
                 }
                 return -1;
             }

             $scope.$on('fwatable.checked', function(e,d){
                 if(findCheck(d) == -1) {
                     $scope.checkRows.push(d);
                 }
             });

             $scope.$on('fwatable.unchecked', function(e,d){
                 var index = findCheck(d);
                 if(index != -1) {
                     $scope.checkRows.splice(index,1);
                 }
             });
             
             
      
        
         /**
          * 批量提交稽核审核记录
          */
          $scope.batchSumit=function(){
        	  $scope.selectList=[{code:'1',label:'同意'},{code:'0',label:'重新稽核'}];
        	  $scope.auditObj={};
        	  if($scope.checkRows.length>0){
        		  
        		    var flag=false;
        		    
        		 	for(var i=0;i< $scope.checkRows .length;i++) {
                        var checkedData =  $scope.checkRows [i];
                        var caseStatus=checkedData.caseStatus;
                        if(caseStatus!='5'){ //不是审核状态
                        	flag=true;
                        }
                    }
        		if(flag){
        		 		 Modal.alert("只能选择审核状态进行批量操作!");
               		      return;
        		}else{
        			   popDialogFun();
        		     }
        			   
        		 
        	  }else{
        		  Modal.alert("请勾选要批量提交的记录!");
        		  return;
        	      }
          }
          
         /**
          * 批量操作弹出框
          */
         function popDialogFun(){
        	 var scope = $scope.$new();
     		var modalInstance = $modal.open({
     		      templateUrl: baseUrl+'views/hcpms/audit/caseCheck/caseCheckBatchSubmitDialog_view.html',
     		      backdrop:'static',
     			  size:'sm',
     			  scope:scope,
     			  controller: function($scope, $http,$modalInstance,Modal){
     				  
     				  
     				     //确定事件
	     				 $scope.batchConfirm=function(){
	     					 
	     					 if($scope.auditObj.opinion){
	     						 
	     						  if($scope.auditObj.opinion=='0'){
		     				    	    if(!$scope.auditObj.opinionDesc){
		     				    	    	Modal.alert('意见描述不能为空!');
		     				    	    	return;
		     				    	    }
		     				       }
	     						 
	     					 }else{
	     						Modal.alert('请选择审核意见!');
 				    	    	return; 
	     					 }
	     					 
	     				     
	     				       
	     				      var batchCaseCode=new Array();
	     				 	
	     				      for(var i=0;i< $scope.checkRows .length;i++) {
	     				    	 var checkedData =  $scope.checkRows [i];
	     				    	 var currentCaseCode=checkedData.caseCode;
	     				    	 batchCaseCode.push(currentCaseCode);
	     				 	    }
	     				      var obj={
	     				    		 caseCodes:batchCaseCode,
	     				    		opinion:$scope.auditObj.opinion,
	     				    		opinionDesc:$scope.auditObj.opinionDesc
	     				    		
	     				      };
	     				     submitSelectAllRecord(obj,$scope.onClose);
	     				 }
     				  
     				  
     					//关闭model
     					$scope.onClose = function() {
     						$modalInstance.close();
     					}
     					
     					
     			     }
     		      });
     			  
     		}
         
         
         
         /**
          * 开始提交批量选择的记录
          */
         function submitSelectAllRecord(parameter,closeFun){
        	 $scope.loading = true;
        	 $http({
 				method : 'post',
 				cache : false,
 				url : baseUrl + 'caseCheckAudit/caseBatchSubmit.do',
 				params :{
 					caseCodes:parameter.caseCodes,
 					opinion:parameter.opinion,
 					opinionDesc:parameter.opinionDesc
 				}
 			}).success(function(response, status, headers, config) {
 			    $scope.loading = false;
 				  if(response.flag){
 					   closeFun();
 					  $scope.onSubmit();
 					  Modal.alert(response.message);
 				  }else{
 					 closeFun();
 					 Modal.alert(response.message);
 				  }
 			});
        	 
        	 
        	 
         }
         
   
        
        /**
	     * 
	     **/
	    $scope.onSubmit = function(resetPage){
	    	$scope.loading = true;
            if(resetPage == null || !resetPage){
	    		$scope.currentPage= 1;
	    		$scope.params.page = 0; 
	    	}else{
    			$scope.params.page = ($scope.currentPage - 1) * 20;
	    	}
            
            var fparam = $scope.params;
            if($scope.params.auditDate){
                var start = $scope.params.auditDate.start;
                var end = $scope.params.auditDate.end;
                fparam.beginDate = start;
                fparam.endDate = end;
            }
            if($scope.params.sbmitDate){
                var start = $scope.params.sbmitDate.start;
                var end = $scope.params.sbmitDate.end;
                fparam.start = start;
                fparam.end = end;
            }
	    	fparam.caseStatus="\'5\',\'6\'";//稽核待审核,稽核待审批
            CaseCommonService.loadCaseTask(fparam).success(function(rst){
            	$scope.loading = false;
	    		if(rst){
	    			
                    $scope.tableData = rst.root;
                    $scope.totalCount = rst.total;
	    		}
	    	});
	    }
	    $scope.onSubmit();
		
	    
	    /**
	     * 案件撤销逻辑处理
	     */
	    $scope.auditCancelProcess=function(caseCode){
	    	$http({
				method : 'post',
				cache : false,
				url : baseUrl + 'caseCheckAudit/auditCancelProcess.do',
				params :{
					caseCode:caseCode
				}
			}).success(function(response, status, headers, config) {
				 var flag=response.flag;
				 var message=response.messsage;
				 if(flag){
					  $scope.onSubmit();
				 }else{
					 Modal.alert(message);
				 }
			});
	    	
	    }    
		

	
	}]);
})();
